package com.ceshiren.Pages;

import io.qameta.allure.Allure;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.util.List;

public class BasePage {
    public static WebDriver driver;
    public static Logger logger= LoggerFactory.getLogger(BasePage.class);
    public BasePage(WebDriver basedriver) {
        driver=basedriver;
    }
    public BasePage(String baseurl) {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        driver=new ChromeDriver(options);
        waitforHide(3);
        driver.get(baseurl);
        driver.manage().window().maximize();
    }
    public void quit(){
        driver.quit();
    }
    //封装显式等待
    public WebDriverWait waitforShow(int second){
        logger.info("显示等待"+second+"s=>++++++");
        return new WebDriverWait(driver, Duration.ofSeconds(second));
    }
    //封装隐式等待
    public void waitforHide(int second){
        logger.info("隐式等待"+second+"s=>++++++");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(second));
    }
    //封装流等待
    public FluentWait<WebDriver> waitforflue(){
        logger.info("流等待=>++++++");
        FluentWait<WebDriver> fluentwait = new FluentWait<>(driver)
                .withTimeout(Duration.ofSeconds(30))
                .pollingEvery(Duration.ofSeconds(5))
                .ignoring(NoSuchElementException.class);
        return fluentwait;
    }
    //封装log
    public void log(String info){
        logger.info("打印info信息=>"+info);
    }
    //封装find方法
    public WebElement find(By by){
        logger.info("查找控件=>"+by);
        return driver.findElement(by);
    }
    //封装find方法
    public List<WebElement> finds(By by){
        logger.info("查找控件=>"+by);
        return driver.findElements(by);
    }

    //截图
    public void shot()  {
        try {
            //生成时间戳
            long nowtime=System.currentTimeMillis();
            String picname="./files/"+nowtime+".png";
            //生成截图
            File currentpic=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(currentpic,new File(picname));
            //保存截图
            Allure.addAttachment("picture","image/png",new FileInputStream(picname),".png");
            //Allure.addAttachment("picture","image/png",Files.newInputStream(Paths.get(picname)),".png");
        } catch (IOException e) {
            logger.warn("截图错误"+e);
        }
    }
    //获取page source
    public void getPageSource(){
        try {
            String pageSource = driver.getPageSource();
            FileWriter fileWriter = null;
            fileWriter = new FileWriter("./files/page_source.text");
            fileWriter.write(pageSource);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
